import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.*;


public class TestZaehler implements ActionListener {
	private JFrame gui;
	private JButton start1, start2;
	private JButton stop1, stop2;
	private JButton halt1, halt2;
	private JLabel zahl1, zahl2, uhr1;
	private JPanel panel1, panel2;
	
	private Zaehler zaehler1, zaehler2;
	private DigitaleUhr uhr;
	
	public static void main(String[] args) {
		new TestZaehler();
	}
	
	public TestZaehler() {
		start1 = new JButton("Start");
		start2 = new JButton("Start");
		stop1 = new JButton("Stop");
		stop2 = new JButton("Stop");
		halt1 = new JButton("Halt");
		halt2 = new JButton("Halt");
		zahl1 = new JLabel();
		zahl2 = new JLabel();
		uhr1 = new JLabel();
		panel1 = new JPanel();
		panel2 = new JPanel();
		gui = new JFrame();
		
		gui.setLayout(new BorderLayout());
		
		panel1.setLayout(new FlowLayout());
		panel2.setLayout(new FlowLayout());
		
		panel1.add(start1);
		panel1.add(stop1);
		panel1.add(halt1);
		panel1.add(zahl1);
		panel1.add(uhr1);
		
		panel2.add(start2);
		panel2.add(stop2);
		panel2.add(halt2);
		panel2.add(zahl2);
		
		gui.add(panel1, BorderLayout.NORTH);
		gui.add(panel2, BorderLayout.SOUTH);
		
		start1.addActionListener(this);
		start2.addActionListener(this);
		stop1.addActionListener(this);
		stop2.addActionListener(this);
		halt1.addActionListener(this);
		halt2.addActionListener(this);
		
		uhr = new DigitaleUhr(uhr1);
		uhr.start();
		
		gui.setSize(500, 110);
		gui.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {

		if(e.getSource() == start1) {
			if(zaehler1 == null) {
				zaehler1 = new Zaehler(zahl1);
				zaehler1.halt(false);
				zaehler1.start();
			}
			else {
				zaehler1.halt(false);
			}
		}
		else if(e.getSource() == start2) {
			if(zaehler2 == null) {
				zaehler2 = new Zaehler(zahl2);
				zaehler2.halt(false);
				zaehler2.start();
			}
			else {
				zaehler2.halt(false);
			}
		}
		else if(e.getSource() == stop1) {
			if(zaehler1 != null) {
				zaehler1.interrupt();
				zaehler1 = null;
			}
			
		}
		else if(e.getSource() == stop2) {
			if(zaehler2 != null) {
				zaehler2.interrupt();
				zaehler2 = null;
			}
		}
		else if(e.getSource() == halt1) {
			if(zaehler1 != null) {
				zaehler1.halt(true);
			}
			
		}
		else if(e.getSource() == halt2) {
			if(zaehler2 != null) {
				zaehler2.halt(true);
			}
		}
	}
}
